//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.Soft;
import cn.nubia.admin.application.bean.SoftRecRank;
import cn.nubia.admin.application.bean.SoftRecRankVo;
import cn.nubia.admin.application.query.SoftRecRankQuery;
import cn.nubia.admin.application.service.SoftService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.bean.Query.DBOrder;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.SystemLog.ModuleTypeEnum;
import cn.nubia.admin.sysconfig.bean.SystemLog.OperateTypeEnum;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SoftRecRankService extends BaseServiceImpl<SoftRecRank> {
    @Autowired
    SystemLogService systemLogService;
    @Autowired
    SoftService softService;

    public SoftRecRankService() {
    }

    public Paged<SoftRecRankVo> findPage(SoftRecRankQuery softRecRankQuery) {
        Query query = Query.build(SoftRecRank.class);
        query.setPaged(softRecRankQuery.getPageNo(), softRecRankQuery.getPageSize());
        if(softRecRankQuery.getState() != null && softRecRankQuery.getState() != -99)
        {
            query.addEq("state", softRecRankQuery.getState());
        }
        else{
            query.addEq("state", SoftRecRank.STATE_SHOW);
        }
        if(softRecRankQuery.getSoftId() != null) {
            query.addEq("softId", softRecRankQuery.getSoftId());
        }

        if(softRecRankQuery.getStartTimes() != null && softRecRankQuery.getStartTimee() != null) {
            query.addBetween("startTime", DateUtil.getIntegerTime(softRecRankQuery.getStartTimes()), DateUtil.getIntegerTime(softRecRankQuery.getStartTimee()));
        }

        query.addOrder("startTime", DBOrder.DESC);
        Paged page = this.findPagedByQuery(query);
        List list = page.getListData();
        Paged paged = new Paged();
        ArrayList listd = new ArrayList();

        for(int i = 0; i < list.size(); ++i) {
            SoftRecRank softRecRank = (SoftRecRank)list.get(i);
            SoftRecRankVo softRecRankVo = new SoftRecRankVo();
            BeanUtils.copyProperties(softRecRank, softRecRankVo);
            softRecRankVo.setSoftName(((Soft)this.softService.get(softRecRank.getSoftId())).getSoftName());
            listd.add(softRecRankVo);
        }

        paged.setListData(listd);
        paged.setPageSize(page.getPageSize());
        paged.setPageNo(page.getPageNo());
        paged.setTotalCount(page.getTotalCount());
        return paged;
    }

    public String save(SoftRecRank softRecRank) {
        Integer currTime = DateUtil.getCurrentTime();
        softRecRank.setLastModifyTime(currTime);
        if(null == softRecRank.getId()) {
            Query query = Query.build(SoftRecRank.class);
            query.addEq("softId", softRecRank.getSoftId());
            query.addEq("state",0);
            if(this.get(query) != null) {
                return "添加的推荐应用已经存在";
            }

            this.insert(softRecRank);
            this.systemLogService.log(ModuleTypeEnum.SOFTRECRANK, OperateTypeEnum.ADD, softRecRank.getId());
        } else {
            SoftRecRank s = get(softRecRank.getId());
            if (s != null)
                softRecRank.setState(s.getState());
            this.update(softRecRank);
            this.systemLogService.log(ModuleTypeEnum.SOFTRECRANK, OperateTypeEnum.EDIT, softRecRank.getId());
        }

        return "";

    }

    public void changeSoftRecRank(Integer softRecRankId, Integer state) {
        SoftRecRank softRecRank = (SoftRecRank)this.get(softRecRankId);
        softRecRank.setState(state);
        this.update(softRecRank);
        if(state.intValue() == -1) {
            this.systemLogService.log(ModuleTypeEnum.SOFTRECRANK, OperateTypeEnum.DEL, softRecRank.getId());
        } else if(state.intValue() == 0) {
            this.systemLogService.log(ModuleTypeEnum.SOFTRECRANK, OperateTypeEnum.SHOW, softRecRank.getId());
        }

    }
}
